define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'category/index',
                    add_url: 'category/add',
                    edit_url: 'category/edit',
                    del_url: 'category/del',
                    multi_url: 'category/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pagination: false,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), operate: false, searchList: Config.searchList, formatter: Table.api.formatter.label},
                        {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        article: function() {
            Table.api.init({
                extend: {
                    index_url: "category/article" + location.search,
                    add_url: "category/add?type=article",
                    edit_url: "category/edit?type=article",
                    del_url: "category/del",
                    multi_url: "category/multi",
                    table: "category"
                }
            });
            var e = $("#table");
            e.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: "id",
                sortName: "weigh",
                pagination: !1,
                columns: [[{
                    checkbox: !0
                },
                {
                    field: "id",
                    title: __("Id")
                },
                {
                    field: "name",
                    title: __("Name"),
                    align: "left",
                    formatter: Controller.api.formatter.escape2Html
                },
                {
                    field: "type",
                    title: __("Type"),
                    custom: {
                        article: "info",
                        goods: "success"
                    },
                    formatter: Table.api.formatter.flag
                },
               
                {
                    field: "status",
                    title: __("Status"),
                    searchList: {
                        normal: __("Normal"),
                        hidden: __("Hidden")
                    },
                    formatter: Table.api.formatter.status
                },
                {
                    field: "id",
                    title: __("展开"),
                    operate: !1,
                    formatter: Controller.api.formatter.subnode
                },
                // {
                //     field: "isnavswitch",
                //     title: __("isnavswitch"),
                //     searchList: {
                //         1 : __("Yes"),
                //         0 : __("No")
                //     },
                //     formatter: Table.api.formatter.toggle
                // },
                {
                    field: "operate",
                    title: __("Operate"),
                    table: e,
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate
                }]],
                search: !1,
                commonSearch: !1
            }),
            e.on("post-body.bs.table",
            function(e, a, i, o) {
                $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").hide(),
                $(".btn-node-sub").off("click").on("click",
                function(e) {
                    var a = !!($(this).data("shown") || $("a.btn[data-pid='" + $(this).data("id") + "']:visible").size() > 0);
                    return $("a.btn[data-pid='" + $(this).data("id") + "']").each(function() {
                        $(this).closest("tr").toggle(!a),
                        $(this).hasClass("disabled") || $(this).trigger("click")
                    }),
                    $(this).data("shown", !a),
                    !1
                })
            }),
            $(document.body).on("click", ".btn-toggle",
            function(e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var a = this,
                i = $("i", a).hasClass("fa-chevron-down");
                $("i", a).toggleClass("fa-chevron-down", !i),
                $("i", a).toggleClass("fa-chevron-up", i),
                $("a.btn[data-id][data-pid][data-pid!=0]").not(".disabled").closest("tr").toggle(i),
                $(".btn-node-sub[data-pid=0]").data("shown", i)
            }),
            $(document.body).on("click", ".btn-toggle-all",
            function(e) {
                var a = this,
                i = $("i", a).hasClass("fa-plus");
                $("i", a).toggleClass("fa-plus", !i),
                $("i", a).toggleClass("fa-minus", i),
                $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").toggle(i),
                $(".btn-node-sub[data-pid!=0]").data("shown", i)
            }),
            Table.api.bindevent(e)
        },
        api: {
            formatter: {
                subnode: function(t, e, a) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __("Toggle sub menu") + '" data-id="' + e.id + '" data-pid="' + e.pid + '" class="btn btn-xs ' + (1 == e.haschild || 1 == e.ismenu ? "btn-success": "btn-default disabled") + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>'
                },
                route: function(t, e, a) {
                    return "/pages/product/list?data=" + {
                        category_id: e.id,
                        category_name: e.name.replace(e.spacer, "")
                    }
                },
                escape2Html: function(t, e, a) {
                    return t.toString().replace(/(&|&amp;)nbsp;/g, "")
                },
                icon: function (value, row, index) {
                    return '<span class="' + (row.type == 'material' || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
                },
                
            },
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
